package com.zzyl.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @Author mr.zhang
 * @Date 2025/7/26 10:04
 */
@SpringBootTest
public class RedisTest {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Test
    public void test(){
        System.out.println(redisTemplate);
    }
    @Test
    public void testString(){
        redisTemplate.opsForValue().set("name","张三");
        System.out.println(redisTemplate.opsForValue().get("name"));
        System.out.println(redisTemplate.opsForValue().get("token"));

        // setnx 当指定的键key不存在时,会将key的值设置为value，返回true，否则返回false(不能覆盖原有值)
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "09876", 5, TimeUnit.MINUTES));
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "34567", 5, TimeUnit.MINUTES));
    }
    @Test
    public void testZset(){
        redisTemplate.opsForZSet().add("zset", "张三", 1);
        redisTemplate.opsForZSet().add("zset", "张三1", 2);
        redisTemplate.opsForZSet().add("zset", "张三2", 3);
        Set<String> zset = redisTemplate.opsForZSet().range("zset", 0, -1);
        System.out.println(zset);
        redisTemplate.opsForZSet().incrementScore("zset", "张三", 1);
        redisTemplate.opsForZSet().remove("zset", "张三1", "张三2");
    }
}
